package hz.mobileproject.pda.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.OnSureLisener;
import com.codbking.widget.bean.DateType;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.piterwilson.audio.MP3RadioStreamPlayer;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;
import hz.mobileproject.pda.R;
import hz.mobileproject.pda.entity.request.AllTalk;
import hz.mobileproject.pda.entity.response.AllTalkResPonse;
import hz.mobileproject.pda.presenter.AllTalkp;
import hz.mobileproject.pda.ui.adapter.AllTalkListAdapter;

/**
 * Created by zmybi on 2017/8/24.
 */

public class AllTalkActivity extends XActivity<AllTalkp> {

    private static final String TAG = "AllTalkActivity";
    private int page = 1;
    private String mCusnumber;
    private String mQueryTime;
    private String mQueryDept;
    private RecyclerView mRecyclerView;
    private SmartRefreshLayout mSmartRefreshLayout;

    private List<AllTalkResPonse> mAllTalkResPonses = new ArrayList<>();
    private AllTalkListAdapter mAllTalkListAdapter;
    //选择的时间
    private String mSelectQueryTime;
    private MP3RadioStreamPlayer mp3Player;


    @Override
    public void onWidgetClick(View view) {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_nontalk);
        mSmartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.smartLayout);
        SPUtils spUtils = SPUtils.getInstance("app_login");
        mp3Player = new MP3RadioStreamPlayer();

        mCusnumber = spUtils.getString("cusnumber");
        mQueryTime = TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM"));
//        mQueryTime = TimeUtils.getNowString();
        mQueryDept = spUtils.getString("department_id");
        final String queryPage = page + "";


        //点击选择时间
        Button bt_select_time = (Button) findViewById(R.id.bt_select_time_all);
        bt_select_time.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickDialog dialog = new DatePickDialog(AllTalkActivity.this);
                //设置上下年分限制
                dialog.setYearLimt(5);
                //回显当前选择时间
                if(!StringUtils.isEmpty(mSelectQueryTime)){
                    try {
                        dialog.setStartDate(new SimpleDateFormat("yyyy-MM").parse(mSelectQueryTime));
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                //设置标题
                dialog.setTitle("选择时间");
                //设置类型
                dialog.setType(DateType.TYPE_YM);
                //设置消息体的显示格式，日期格式
                dialog.setMessageFormat("yyyy-MM-dd HH:mm");
                //设置选择回调
                dialog.setOnChangeLisener(null);
                //设置点击确定按钮回调
                dialog.setOnSureLisener(new OnSureLisener() {
                    @Override
                    public void onSure(Date date) {
                        LogUtils.e(date);
                        page = 1;
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                        mSelectQueryTime = sdf.format(date);
                        mQueryTime = mSelectQueryTime;
                        LogUtils.e(mSelectQueryTime);    //1974-01
                        //点击选择时间后 重新请求网络
                        AllTalk selectAllTalk = new AllTalk(mCusnumber, mQueryTime, mQueryDept,queryPage);
                        LogUtils.e("select",selectAllTalk);
//                        Gson gson = new Gson();
//                        String selectJson = gson.toJson(selectTalkPush);
                        getP().setData(selectAllTalk,3);

                    }
                });
                dialog.show();


            }
        });


        AllTalk allTalk = new AllTalk(mCusnumber,mQueryTime,mQueryDept,queryPage);

        getP().setData(allTalk,1);

        mSmartRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                ToastUtils.showLong(page + "");
                AllTalk allTalk = new AllTalk(mCusnumber, mQueryTime, mQueryDept,page + "");
                getP().setData(allTalk,2);
            }

        });



    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_alltalk;
    }

    @Override
    public AllTalkp newP() {
        return new AllTalkp();
    }

    public void setView(String json, int i) {
        switch (i) {
            case 1: {
                LogUtils.e(TAG,json);
                mAllTalkResPonses = new Gson().fromJson(json,new TypeToken<List<AllTalkResPonse>>(){}.getType());
                mRecyclerView.setLayoutManager(new LinearLayoutManager(AllTalkActivity.this));
                mAllTalkListAdapter = new AllTalkListAdapter(AllTalkActivity.this,mAllTalkResPonses);
                mRecyclerView.setAdapter(mAllTalkListAdapter);

                mAllTalkListAdapter.setOnPlayBtnClick(onMyBtnClick);


            }
            break;
            case 2: {
                mSmartRefreshLayout.finishLoadmore();
                LogUtils.e(TAG,json);
                //第二页的数据
                List<AllTalkResPonse> newnoneTalkListBeans = new Gson().fromJson(json,new TypeToken<List<AllTalkResPonse>>(){}.getType());
                if(newnoneTalkListBeans == null) {
                    ToastUtils.showLong("没有更多数据");
                    return;
                }

                mAllTalkListAdapter.addData(newnoneTalkListBeans);

            }
            break;
            //选择了时间
            case 3: {
                LogUtils.e(TAG,json);
                mAllTalkResPonses = new Gson().fromJson(json,new TypeToken<List<AllTalkResPonse>>(){}.getType());
                if(mAllTalkResPonses == null) {
                    ToastUtils.showLong("无记录");
                }
                mAllTalkListAdapter.setData(mAllTalkResPonses);


            }
            break;
        }
    }


    private AllTalkListAdapter.OnBtnClick onMyBtnClick = new AllTalkListAdapter.OnBtnClick() {
        @Override
        public void playRecord(String url) {
            LogUtils.e(url);
            if(mp3Player.getState() == MP3RadioStreamPlayer.State.Playing) {
                mp3Player.stop();
            }
            mp3Player = new MP3RadioStreamPlayer();
            mp3Player.setUrlString(AllTalkActivity.this,true,url);
            try {
                mp3Player.play();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void pausePlayRecord() {
            if(mp3Player.isPause()) {
                return;
            }
            mp3Player.setPause(true);
        }

        @Override
        public void stopPlayRecord() {
            mp3Player.stop();
        }

        @Override
        public void reStartPlayRecord() {
            if(!mp3Player.isPause()){
                return;
            }
            mp3Player.setPause(false);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mp3Player.stop();
        mp3Player = null;
    }
}
